"ui";

var color = "#009688";
var myPath = String(engines.myEngine().getSource()).replace("main.js", "");
ui.layout(
    // 垂直布局
    <vertical>

        <appbar>
            <toolbar id="toolbar" title="大管家" />
            <tabs id="tabs" />
        </appbar>

        <viewpager id="viewpager">

            <vertical>

                <card w="*" h="40" margin="10" cardCornerRadius="2dp"
                    cardElevation="1dp" gravity="center_vertical">

                    <Switch id="autoService" text="无障碍服务" checked="{{auto.service != null}}" padding="18 8 8 8" textSize="15sp" />

                    <View bg="#4caf50" h="*" w="10" />
                </card>

                <card w="*" h="40" margin="10 1" cardCornerRadius="2dp"
                    cardElevation="1dp" gravity="center_vertical">
                    <horizontal>

                        <text text="功能选择" padding="18 8 8 8" textSize="15sp" gravity="center_vertical" textColor="black" />


                        <spinner id="sp1" entries="异变合兽|任务一条龙|选项3" textColor="blue" marginLeft="20" />

                    </horizontal>


                    <View bg="#ff00ff" h="*" w="10" />
                </card>

                <button id="start" text="开始运行" style="Widget.AppCompat.Button.Colored" w="*" margin="10" />

                <card w="*" h="*" margin="10 5" cardCornerRadius="2dp"
                    cardElevation="1dp" gravity="center_vertical">
                    <scroll>

                        <vertical padding="10">



                        </vertical>

                    </scroll>
                </card>


            </vertical>



            <frame>
                <text text="第二页内容" textColor="red" textSize="16sp" />
            </frame>

            <frame>
                <text text="第三页内容" textColor="green" textSize="16sp" />
            </frame>

            <vertical>
                <button w="*" id="activation" text="未激活，点击激活" textSize="12sp" textColor="black" style="Widget.AppCompat.Button.Borderless.Colored" />
            </vertical>

        </viewpager>

    </vertical>

);


//创建选项菜单(右上角)
// ui.emitter.on("create_options_menu", menu=>{
// menu.add("设置");
// menu.add("关于");
// });

//监听选项菜单点击
ui.emitter.on("options_item_selected", (e, item) => {
    switch (item.getTitle()) {
        case "设置":
            toast("还没有设置");
            break;
        case "关于":
            alert("关于", "Auto.js界面模板 v1.0.0");
            break;
    }
    e.consumed = true;
});

activity.setSupportActionBar(ui.toolbar);

//设置滑动页面的标题
ui.viewpager.setTitles(["首页", "第二页", "第三页", "第四页"]);

//让滑动页面和标签栏联动
ui.tabs.setupWithViewPager(ui.viewpager);


// var tiao = require("跳兽.js");
// var circle = require('circle.js')

ui.autoService.on("check", function (checked) {

    if (checked && auto.service == null) {
        app.startActivity({
            action: "android.settings.ACCESSIBILITY_SETTINGS"
        });
    }
    if (!checked && auto.service != null) {
        auto.service.disableSelf();
    }
});

// 当用户回到本界面时，resume事件会被触发
ui.emitter.on("resume", function () {
    // 此时根据无障碍服务的开启情况，同步开关的状态
    ui.autoService.checked = auto.service != null;
});


//点击开始运行按钮
ui.start.on("click", function () {
    //程序开始运行之前判断无障碍服务
    if (auto.service == null) {
        toast("请先开启无障碍服务！");
        return;
    }


    var index = ui.sp1.getSelectedItemPosition();


    main(index);

});

function main(index) {
    // 这里写脚本的主逻辑
    threads.start(function () {

        if (index == 0) {
            toastLog("启动跳兽")
            // 跳兽.跳兽主函数();
            // console.log("半径为 4 的圆的面积是 %d", circle.area(4));
            // tiao.找图点击("采集.png",0,0);
            engines.execScriptFile(myPath + "跳兽.js");
        }
        if (index == 1) {
            打开应用("QQ")
        }


    });
}
